package cn.iocoder.yudao.module.member.controller.admin.profit;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.module.member.controller.admin.profit.vo.ProfitMainPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.profit.vo.ProfitMainRespVO;
import cn.iocoder.yudao.module.member.dal.dataobject.profit.ProfitMainDO;
import cn.iocoder.yudao.module.member.service.profit.ProfitMainService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import java.util.*;
import java.io.IOException;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;


@Tag(name = "管理后台 - 利润报")
@RestController
@RequestMapping("/sys/profit-main")
@Validated
public class ProfitMainController {
    @Resource
    private ProfitMainService profitMainService;
    @GetMapping("/page")
    @Operation(summary = "获得利润报分页")
    @PreAuthorize("@ss.hasPermission('sys:profit-main:query')")
    public CommonResult<PageResult<ProfitMainRespVO>> getProfitMainPage(@Valid ProfitMainPageReqVO pageReqVO) {
        PageResult<ProfitMainDO> pageResult = profitMainService.getProfitMainPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, ProfitMainRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出利润报 Excel")
    @PreAuthorize("@ss.hasPermission('sys:profit-main:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportProfitMainExcel(@Valid ProfitMainPageReqVO pageReqVO,
                                      HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<ProfitMainDO> list = profitMainService.getProfitMainPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "利润报.xls", "数据", ProfitMainRespVO.class,
                BeanUtils.toBean(list, ProfitMainRespVO.class));
    }
}
